package com.sgb.kjwl.viewmodel.manage

import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.manage.AnnouncementListEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.H5ReEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.util.MToast
import retrofit2.Call

class WebsiteAnnouncementListViewModel (application: Application) : AndroidViewModel(application) {

    private var page = 1
    private val limit = 10

    private val managementMap = HashMap<String, Any>()

    private val listMap = HashMap<String, Any>()

    var mManagementList = MutableLiveData<List<AnnouncementListEntity>>()


    var mWebsiteList = MutableLiveData<List<AnnouncementListEntity>>()

    var mDeletePosition = MutableLiveData<Int>()

    /**
     * 获取广告管理列表(后台)
     */
    fun getAnnouncementManagement(isLoadMore : Boolean){
        managementMap["page"] = if (isLoadMore) page + 1 else 1
        managementMap["limit"] = limit
        managementMap["createUserName"] = ""//发布人
        managementMap["title"] = ""//标题
        managementMap["type"] = ""//类型
        APIManager.getInstance().websiteInfoAPI.getAnnouncementManagement(managementMap).enqueue(object :
            ICallBack<BaseEntity<ListResponse<List<AnnouncementListEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<AnnouncementListEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    mManagementList.postValue(data.data.list)
                } else {
                    mManagementList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<AnnouncementListEntity>>>>,
                t: Throwable
            ) {
                mManagementList.postValue(ArrayList())
            }

        })
    }


    /**
     * 获取广告列表(前台)
     * siteNo 网站no
     * siteType 网站类型
     */
    fun getAnnouncementList(isLoadMore : Boolean, siteNo : String, siteType : String){
        listMap["page"] = if (isLoadMore) page + 1 else 1
        listMap["limit"] = limit
        listMap["siteNo"] = siteNo//网站no
        listMap["siteType"] = siteType//网站类型
        APIManager.getInstance().websiteInfoAPI.getAnnouncementManagement(listMap).enqueue(object :
            ICallBack<BaseEntity<ListResponse<List<AnnouncementListEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<AnnouncementListEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null){
                    mWebsiteList.postValue(data.data.list)
                } else {
                    mWebsiteList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                call: Call<BaseEntity<ListResponse<List<AnnouncementListEntity>>>>,
                t: Throwable
            ) {
                mWebsiteList.postValue(ArrayList())
            }

        })
    }

    /**
     * 删除公告
     */

    fun deleteAnnouncementByNo(no : String, position: Int){
        APIManager.getInstance().websiteInfoAPI.deleteAnnouncementByNo(no).enqueue(object :
            ICallBack<BaseEntity<*>>(true, true) {
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    mDeletePosition.postValue(position)
                } else {
                    mDeletePosition.postValue(-1)
                }
            }

            override fun onFailure(call: Call<BaseEntity<*>>, t: Throwable) {
                mDeletePosition.postValue(-1)
            }

        })
    }

    //跳转h5详情
    fun getH5Url(mContext: Context, no: String, param: String) {
        APIManager.getInstance().pickAPI.getUrlSetting(param)
            .enqueue(object : ICallBack<BaseEntity<H5ReEntity?>?>() {
                override fun onSuccess(data: BaseEntity<H5ReEntity?>?) {
                    if (data == null || data.state == null) {
                        return
                    }
                    if (data.state == APIManager.RESPONSE_OK) {
                        var url = data.data!!.url
                        url = "$url?no=$no"
                        WebViewX5HostActivity.start(mContext, url)
                    } else {
                        MToast.showToast(mContext, data.msg)
                    }
                }
            })
    }

}